package com.hoperun.product.net;

import java.io.IOException;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

import org.json.JSONObject;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import com.blankj.utilcode.util.LogUtils;
import com.hoperun.product.common.Setting;

import android.util.Log;

/**
 * 调用 webservice �?
 *
 * @author wang_donghai
 */
public class WebServiceAccessUtils {

    private static String TAG = "WebServiceAccessUtils";
    /**
     * 调用服务器的方法
     *
     * @param methodName
     * @param param
     * @return
     */
    public static String call(final String methodName, String param) {
        SoapObject request = new SoapObject(Setting.nameSpace, methodName);
        if (param != null) {
            request.addProperty("arg0", param);

            LogUtils.d(TAG, "OnWebSercieRequest **** param=" + param);
            LogUtils.d(TAG, "methodName=" + methodName);
        }

        final SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = false;
        envelope.setOutputSoapObject(request);

        final HttpTransportSE ht = new HttpTransportSE(Setting.url,  Setting.getSocketTimeout());
        ht.debug = true;

        FutureTask<String> future = new FutureTask<String>(new Callable<String>() {

            @Override
            public String call() {
                String OPT_FLAG = "0";
                String MSG_INFO = "";
                try {
                    ht.call(Setting.nameSpace + methodName, envelope);
                    if (envelope.getResponse() != null) {
                        OPT_FLAG = "0";
                        MSG_INFO = envelope.getResponse().toString();
                        LogUtils.d(TAG, "MSG_INFO=" + MSG_INFO);
                    }
                } catch (IOException e) {
                    OPT_FLAG = "1";
                    MSG_INFO = e.toString();
                } catch (XmlPullParserException e) {
                    OPT_FLAG = "1";
                    MSG_INFO = e.toString();
                } catch (Exception e) {
                    OPT_FLAG = "1";
                    MSG_INFO = e.toString();
                }

                String result = "";
                JSONObject retJson = new JSONObject();
                try {
                    retJson.put(Setting.OPT_FLAG, OPT_FLAG);
                    retJson.put(Setting.MSG_INFO, MSG_INFO);
                    result = retJson.toString();
                } catch (Exception e) {
                    // TODO: handle exception
                    result = "{\"OPT_FLAG\":1,\"MSG_INFO\":\"" + e.toString() + "\"}";
                }
                if (OPT_FLAG.equals("1")) {
                    Log.d(TAG, "OnWebSercieRequest **** MSG_INFO=" + MSG_INFO);
                }
                return result;
            }
        });
        new Thread(future).start();
        try {
            return future.get();
        } catch (InterruptedException e) {
            return "{\"OPT_FLAG\":1,\"MSG_INFO\":\"" + e.toString() + "\"}";
        } catch (ExecutionException e) {
            return "{\"OPT_FLAG\":1,\"MSG_INFO\":\"" + e.toString() + "\"}";
        }
    }
}
